#!/bin/bash

export PATH=/bin:/usr/bin:/usr/sbin

BASEPATH=/opt/unixmon/tools/DNS

if [ $# -eq 0 -o $# -gt 2 ]; then
	echo -e "Usage: $(basename $0) -a|-p|-s|\"host[ host] ...\" [file1 [file2...]]"
	exit 
fi

files=""

if [ "$1" == "-a" ]; then
		servers="all"
else
	if [ "$1" == "-s" ]; then
		servers="secondary"
	else
		if [ "$1" == "-p" ]; then
			servers="primary"
		else
	
			servers=$1
			for server in $(echo $servers); do 
				host $server >/dev/null 2>&1
				if [ $? -ne 0 ]; then
					echo "Error: $server cannot be resolved."
					echo -e "Usage: $(basename $0) [-a|-p|-s|\"host[ host] ...\" [file1 [file2...]]]"
					exit 1
				fi
			done
		fi
	fi
fi

files="$2"

if [ ! -f $(dirname $0)/IT_DNS_SERVERS_PHYSICAL ]; then
	echo "Error: $(dirname $0)/IT_DNS_SERVERS_PHYICAL is missing."
	exit 1
fi

. $(dirname $0)/IT_DNS_SERVERS_PHYSICAL

if [ "$servers" == "all" ]; then
	servers="$(echo $PRIMARY $SECONDARY)"
else
	if [ "$servers" == "secondary" ]; then
		servers="$(echo $SECONDARY)"
	else
		if [ "$servers" == "primary" ]; then
			servers="$(echo $PRIMARY)"
		fi
	fi
fi

if [ "X${files}" == "X" ]; then
	files=$(cd $BASEPATH; ls [a-z]*) 
fi

for s in $(echo $servers); do
	echo -e "\nVerfifying tools on ${s}..."
		mkdir /tmp/${s}.$$
		cd /tmp/${s}.$$
		ssh $s "cd /usr/local/sbin; tar cf - $(echo $files)" 2>/dev/null | tar xf -
		res="ok"
		for f in $(echo $files); do 
			printf "%-60s" "	Verifying ${f}..."
			if [ ! -f $f ]; then
				echo "x"
				echo -e "\t\t${s}:/usr/local/sbin/${f} doesn't exist."
				continue;
			fi
			diffs=$(diff ${BASEPATH}/${f} $f 2>/dev/null)
			if [ $? -ne 0 -a "X${diffs}" == "X" ]; then echo "x"
			else
			     if [ "X${diffs}" == "X" ]; then
				echo "v"
			     else	
				echo "x"
				ls -al ${BASEPATH}/${f} /tmp/${s}.$$/$f | sed -e "s/\/tmp\/${s}.$$/${s}:\/usr\/local\/sbin/" | sed -e 's/^/		/'
				res="no"
			     fi
			fi
		done
		if [ $res == "ok" ]; then cd; rm -r /tmp/${s}.$$; fi
done
